<?php

namespace App\Modules\Good\Models;


use App\Modules\Coupon\Models\FullReduction;
use App\Modules\Coupon\Models\FullReductionScope;
use App\Modules\Good\Models\GoodsDiscount;
use App\Modules\Good\Models\GoodsDiscountScope;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Log;

/**
 * Class Good
 * @package App\Modules\Good\Models
 * @property string $category
 * @property string $name
 * @property string $tag
 * @property array $cover
 * @property integer $display_price
 * @property integer $price
 * @property string $description
 * @property integer $sales_volume
 * @property int $visitor
 * @property int $share_number
 * @property boolean $is_free
 * @property boolean $is_crowd
 * @property string $status
 * @property integer $need_delivery
 * @property int $sort
 * @property string $order_cover
 * @property string $share_bg
 * @property string $video
 */
class Good extends Model
{
    use SoftDeletes;

    const STATUE_ON = 'on';
    const STATUE_OFF = 'off';

    static $showStatus = [
        self::STATUE_ON => "上架",
        self::STATUE_OFF => "下架",
    ];

    protected $fillable = [
        'shop_id',
        'category',
        'name',
        'tag',
        'keywords',
        'cover',
        'list_cover',
        'display_price',
        'price',
        'description',
        'sales_volume',
        'is_free',
        'status',
        'visitor',
        'share_number',
        'sort',
        'need_delivery',
        'order_cover',
        'share_bg',
        'video',
        'buy_limit',
        'discount_type',
        'discount_limit',
        'discount_amount',
        'end_time',
        'virtual_sales_volume',
        'category_id',
        'brand_id',
        'top_category_id',
        'first_category_id',
        'is_balance_pay',//是否支持余额支付
        'is_recharge',//本商品是否为充值商品
        'recharge_money',//实际充值金额
        'tag_id',//服务标签
        
    ];

    protected $casts = [
        'cover' => 'array',
    ];


    public function getDiscount($price)
    {
        $scope = GoodsDiscountScope::query()
            ->where('type', self::class)
            ->where('link_id', $this->id)
            ->get();
        $discount = GoodsDiscount::query()
            ->whereIn('id', $scope->pluck('goods_discount_id')->all())
            ->where('limit', "<=", $price)
            ->orderBy('ratio')
            ->first();
        if (is_null($discount)) {
            return 0;
        }
        return $price * (1 - ($discount->ratio / 100));
    }

    public function getFullReduction($price)
    {
        $scope = FullReductionScope::query()
            ->where('type', self::class)
            ->where('link_id', $this->id)
            ->get();
        $reduction = FullReduction::query()
            ->whereIn('id', $scope->pluck('full_reduction_id')->all())
            ->where('limit', "<=", $price)
            ->orderBy('abatement', 'desc')
            ->first();
        if (is_null($reduction)) {
            return 0;
        }
        return $reduction->abatement * floor($price/$reduction->limit);
    }

    public function getGift()
    {
        $scope = GoodsGiftScope::query()
            ->where('goods_id', $this->id)
            ->get();

        $gift = GoodsGift::query()
            ->whereIn('id', $scope->pluck('gift_id')->all())
            ->get();
        if (is_null($gift)) {
            return ;
        }
        $ids = [];
        foreach ($gift as $item) {
            $tmpId = array_column($item->content??[], 'id');
            $ids = array_merge($tmpId, $ids);
        }
        $goods = self::query()->find($ids)->keyBy('id');
        if (is_null($gift)) {
            return null;
        }
        return $gift->map(function ($item) use ($goods) {
            $content = [];
            foreach ($item->content as $value) {
                $tmp = $goods->get($value['id']);
                if (!is_null($tmp)) {
                    $value['price'] = $tmp->price;
                    $content[] = $value;
                }
            }
            $item->content = $content;
            return $item;
        });
    }
}
